//
//  NewFeatureViewController.swift
//  Webo10
//
//  Created by ChengXiwu on 2017/7/30.
//  Copyright © 2017年 ChengXiwu. All rights reserved.
//

import UIKit

private let WBNewFeatureViewCellId = "WBNewFeatureViewCellId"
private let WBNewFeatureImageCount = 4

class NewFeatureViewController: UICollectionViewController {

    init() {
        // 使用FlowLayout,类似于Android中的帧布局
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.main.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        
        super.init(collectionViewLayout: layout)
        
        // 打开分页
        collectionView?.isPagingEnabled = true
        // 弹簧效果,默认打开
        collectionView?.bounces = true
        // 水平分页指示器,默认打开
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 隐藏状态栏
    override var prefersStatusBarHidden: Bool{
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // 注册可重用Cell
        self.collectionView!.register(NewFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellId)
    }

    

    // MARK: - UICollectionViewDataSource数据源方法
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // 页数
        return 4
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: WBNewFeatureViewCellId, for: indexPath) as! NewFeatureCell
    
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.red : UIColor.yellow
        cell.imageIndex = indexPath.item
//        logPrint(cell)
        return cell
    }
    
    override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != WBNewFeatureImageCount - 1{
            return
        }
        
        let cell = collectionView?.cellForItem(at: IndexPath(item: page, section: 0)) as! NewFeatureCell
        cell.showButtonAnim()
    }

}

// MARK: - 新特性Cell
private class NewFeatureCell: UICollectionViewCell{
    
    
    fileprivate var imageIndex: Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_future_\(imageIndex + 1)")
            startButton.isHidden = true
        }
    }
    
    @objc private func clickStartButton(){
        logPrint("开始体验")
        NotificationCenter.default.post(name: WBSwitchRootViewControllerNotification, object: nil)
    }
    
    fileprivate func showButtonAnim(){
        startButton.isHidden = false
        startButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        startButton.isUserInteractionEnabled = false
        
        UIView.animate(withDuration: 1.2,               // 动画时长
                       delay: 0,                        // 延时时间
                       usingSpringWithDamping: 0.7,     // 弹力系数,0-1,越小越弹
                       initialSpringVelocity: 10,       // 初始速度,模拟重力加速度
                       options: [],                     // 动画选项
                       animations: {() in
                            self.startButton.transform = CGAffineTransform.identity
                        }) { (_) in
                            self.startButton.isUserInteractionEnabled = true
                        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(iconView)
        iconView.frame = bounds
        
        addSubview(startButton)
        // 设置按钮位置
        startButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.bottom.equalTo(self.snp.bottom).multipliedBy(0.8)
            make.width.equalTo(200)
            make.height.equalTo(45)
        }
        
        // 设置点击事件
        startButton.addTarget(self, action: #selector(clickStartButton), for: .touchUpInside)
    }
    
    private lazy var iconView: UIImageView = UIImageView()
    
    private lazy var startButton: UIButton = UIButton(title: "开始体验", foreColor: UIColor.white, backgroundImageName: "new_future_finish_button")
}



